package com.itic.hpp.pay;

import com.ctc.wstx.util.StringUtil;
import com.itic.appbase.framework.Base.BaseCode;
import com.itic.mobile.base.BaseMobile;
import com.itic.system.collage.collage.service.CollageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/hpp/pay")
public class PayInterface extends BaseMobile {
    @Autowired
    private CollageService collageService;

    /**
     * 计算实付款
     * @param request
     * @param response
     * @param collageId
     * @param addressId
     * @param count
     * @return
     */
    @RequestMapping(value="/calActualPay")
    @ResponseBody
    public Map<String,Object> calActualPay(HttpServletRequest request, HttpServletResponse response,String collageId,String addressId,Integer count){
        Map<String, Object> resultMap = new HashMap<String,Object>();
        try{
            if(StringUtils.isNotEmpty(collageId) && count !=null){
                //拼团类型(普通(成团价格) 阶梯(定金/补款日期补齐当时成团价格差)) 收货地址 运费模板 拼团Id
                resultMap = this.collageService.calActualPay(collageId,addressId,count);
            }else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        }catch(Exception e){
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("PayInterface", "calActualPay", e.toString(),"计算实付款和运费",e);
        }
        return resultMap;
    }

    /**
     * 支付详情页信息
     * @param request
     * @param response
     * @param userId
     * @param collageId
     * @param count
     * @return
     */
    @RequestMapping(value="/detail")
    @ResponseBody
    public Map<String,Object> detail(HttpServletRequest request, HttpServletResponse response,String userId,String collageId,Integer count){
        Map<String, Object> resultMap = new HashMap<String,Object>();
        try{
            if(StringUtils.isNoneBlank(collageId,userId) && count!=null){
                resultMap =  collageService.getPayDetailVo(userId,collageId,count);
            }else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        }catch(Exception e){
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("PayInterface", "detail", e.toString(),"支付详情页",e);
        }
        isCrrossDomain(response);
        return resultMap;
    }


}
